package com.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.service.OrderSettingService;
import com.healthcommon.entity.MessageConstant;
import com.healthcommon.entity.Result;
import com.healthcommon.pojo.OrderSetting;
import com.healthcommon.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result uplode(@RequestParam("excelFile") MultipartFile multipartFile){
        try{
            List<String[]> lists = POIUtils.readExcel(multipartFile);
            List<OrderSetting> orderSettingList=new ArrayList<OrderSetting>();
            if (lists !=null && lists.size()>0){
                for (String[] strings:lists
                     ) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
            }
            orderSettingService.add(orderSettingList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /*
    * 根据月份查询月份表的信息
    * */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){//参数样式：2019-03
        try {
            List<Map> list=orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
    /*
    * 根据日期修改指定的预约人数
    * */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            //修改预约设置数据成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            //修改预约设置数据失败
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
